---
--类名: CaptainModelRedPoint.lua
--创建日期: 2018-11-10
--创建人: 宋建邦
--功能描述: 船长室红点数据
--修改内容:


local CaptainModelRedPoint = class("CaptainModelRedPoint")

CaptainModelRedPoint.RedPointChange = "CaptainModelRedPointChange"
function CaptainModelRedPoint:ctor()
    self.redpoints = {}
    self.hCollectors = {}
    self.hDelCollectors = {}
    app.session:on("pb_captainRoom.S2C_RedDotNotice",function(ret)
        -- dump(ret, "redpointchange:")
        local typeid = protobuf.enum_id("pb_captainRoom.RedDotType", ret.type)
        if self.redpoints[typeid] and typeid == 2 then --新藏品
            table.insertto(self.redpoints[typeid].Id, ret.Id)
        else
            self.redpoints[typeid] = ret
        end

        if typeid == 2 then
            for i, v in ipairs(ret.Id) do
                self.hCollectors[v] = true
            end
        end

        -- dump(self.hCollectors, "colls:")
        EM:notify(CaptainModelRedPoint.RedPointChange)
    end)
end

function CaptainModelRedPoint:reqRefreshRedPoint(t, cs)
    cs = clone(cs)
    -- dump({type = protobuf.enum_name("pb_captainRoom.RedDotType", t), Id = cs or {}}, "clear arg:")
    if t == 1 or t == 3 then
        if self.redpoints[t] == nil then
            return
        end
    elseif t == 2 then
        if cs == nil or #cs <= 0 then
            return
        end
    end

    local function localRemove()
        if t == 2 and self.redpoints[t] and #self.redpoints[t].Id > 0 then
            for i, v in ipairs(cs) do
                self.hCollectors[v] = nil
                table.removebyvalue(self.redpoints[t].Id, v, true)
            end
        end
    end

    localRemove()
    EM:notify(CaptainModelRedPoint.RedPointChange)

    app.session:request("pb_captainRoom.C2S_ReadRedDotNotice",{type = protobuf.enum_name("pb_captainRoom.RedDotType", t), Id = cs or {}},function(msg)
        -- dump(msg, "response:")
        if msg.ok then
            localRemove()
    
            if t ~= 2 then
                self.redpoints[t] = nil
            end

            EM:notify(CaptainModelRedPoint.RedPointChange)
        end
	end)
end

function CaptainModelRedPoint:getMessageRedPointStamp()
    if self.redpoints[3] then
        return self.redpoints[3].stamp
    end 
    return 0
end

function CaptainModelRedPoint:getVisitorRedPointStamp()
    if self.redpoints[1] then
        return self.redpoints[1].stamp
    end 
    return 0
end

function CaptainModelRedPoint:getRedPointCollectors()
    if self.redpoints[2] then
        return self.redpoints[2].Id
    end

    return {}
end

function CaptainModelRedPoint:getRedPointCollectorsByBagType(t)
    local ret = {}

    if self.redpoints[2] then
        local ctl = app:getInst("CaptainController")
        for i, v in ipairs(self.redpoints[2].Id) do
            local cfg = ctl.hCollectors[v]
            if cfg and checknumber(cfg.bagType) == t then
                ret[#ret + 1] = v
            end
        end
    end
    return ret
end

--本地消除
function CaptainModelRedPoint:delCollectRedPoint(cid)
    self.hDelCollectors[cid] = true
end

function CaptainModelRedPoint:getFilterRedPointCollectorsByBagType(t)
    local cs = self:getRedPointCollectorsByBagType(t)
    local ret = {}
    for i, v in ipairs(cs) do
        if self.hDelCollectors[v] == nil then
            ret[#ret + 1] = v
        end
    end

    return ret
end

function CaptainModelRedPoint:getRedPointCollectorsByType(t)
    local ret = {}

    if self.redpoints[2] then
        local ctl = app:getInst("CaptainController")
        for i, v in ipairs(self.redpoints[2].Id) do
            local cfg = ctl.hCollectors[v]
            if cfg and checknumber(cfg.type) == t then
                ret[#ret + 1] = v
            end
        end
    end
    return ret
end

function CaptainModelRedPoint:reqClearCollectRedPointByBagType(t)
    local cs = self:getRedPointCollectorsByBagType(t)
    if #cs > 0 then
        self:reqRefreshRedPoint(2, cs)
    end
end

function CaptainModelRedPoint:reqClearCollectRedPointByLocationId(lid)
    local ctl = app:getInst("CaptainController")
    if not ctl then
        return
    end

    if ctl.hLocations[lid] then
        self:reqClearCollectRedPointByType(checknumber(ctl.hLocations[lid].type))
    end
end

function CaptainModelRedPoint:reqClearCollectRedPointByType(t)
    local cs = self:getRedPointCollectorsByType(t)
    if #cs > 0 then
        self:reqRefreshRedPoint(2, cs)
    end
end

function CaptainModelRedPoint:reqClearCollectRedPoint()
    if self.redpoints[2] and #self.redpoints[2].Id > 0 then        
        self:reqRefreshRedPoint(2, self.redpoints[2].Id)
    end
end

function CaptainModelRedPoint:reqClearMessageRedPoint()
    if self:getMessageRedPointStamp() > 0 then
        app:sendMsg("CaptainModelRedPoint", "reqRefreshRedPoint", 3)
    end

    if self:getVisitorRedPointStamp() > 0 then
        app:sendMsg("CaptainModelRedPoint", "reqRefreshRedPoint", 1)
    end    
end

function CaptainModelRedPoint:getCollectorRedPoint(id)
    return self.hCollectors[id]
end

function CaptainModelRedPoint:getLocationsRedPoint(lids)
    local types = {}
    local ctl = app:getInst("CaptainController")
    if not ctl then
        return false
    end
    
    for k, v in pairs(lids) do
        if ctl.hLocations[v] then
            table.insert(types, checknumber(ctl.hLocations[v].type))
        end
    end

    for k, v in pairs(self.hCollectors) do
        if table.indexof(types, checknumber(ctl.hCollectors[k].type)) then
            return true
        end
    end
    return false
end

function CaptainModelRedPoint:captainRoomHasRedPoint()
    return self:getMessageRedPointStamp() > 0 or self:getVisitorRedPointStamp() > 0 or #self:getRedPointCollectors() > 0
end

return CaptainModelRedPoint